import { OGraphics, OSprite, OText, ox } from "oixi2";
import { Graphics, Sprite, Text, Texture } from 'pixi.js';
import { COLOR_DEEP_ORANGE, WIDTH } from '../game/constant';
import { getCardById } from '../levels/cardInfo';
import { builtinModules } from "module";
import { BelongType } from "../model/types";


export function LevelItem(attributes: string) {
 
  return ox(new XLevelItem, attributes, () => [
    OGraphics('#highlight alpha=0.5').beginFill(COLOR_DEEP_ORANGE).drawRoundedRect(0, 0, 80, 80, 5).endFill(),
    OText('#strLabel x=10 y=10', '0', { fill: COLOR_DEEP_ORANGE, fontSize: 20 }),
    OText('#costLabel x=30 y=0', '0', { fill: COLOR_DEEP_ORANGE, fontSize: 20 }),
    OText('#nameLabel x=40 y=50 anchor=0.5 ', '0', {align:"center", fill: COLOR_DEEP_ORANGE, fontSize: 12,wordWrap: true,wordWrapWidth:70 ,breakWords:true }),
  ])
}

export class XLevelItem extends Sprite {
  nameLabel: Text = null
  strLabel:Text = null
  btnClose:Sprite = null
  highlight:Graphics = null
  private _id: number
  index:number
  costLabel:Text = null
  isSelect:boolean = false
  belongType:BelongType = BelongType.Player

  constructor() {
    super(Texture.from('back_item.png'))
  }

  selected(){
    if(this.id<=0 || !this.id){
      return
    }
   
    this.parent.children.forEach(v=>{
      v.disSelected()
    })
    this.highlight.visible =true
    this.highlight.alpha = 0.5
  }

  disSelected(){
    this.highlight.alpha=0;
  }

  hideThis(){
    this.visible = false;
  }

  updateStr(extraStr:number=0){
    if(this.id<=0){
      return
    }
    console.log("id:;;"+this.id)
    let cardInfo= getCardById(this.id)
    this.strLabel.text = (Number(cardInfo.strenth)+extraStr).toString()
  }

  updateId(id: number, enable: boolean=true,extraStr:number=0) {
    this.highlight.visible=false
    this._id = id
    let cardInfo= getCardById(id)
    if(cardInfo){
      this.nameLabel.text = cardInfo.name.toString()
      this.strLabel.text = (Number(cardInfo.strenth)+extraStr).toString()
      this.interactive = enable
      this.alpha = enable ? 1 : 0.8
      this.costLabel.text = cardInfo.cost.toString()
    }else{
      this.nameLabel.text = ''
      this.strLabel.text= ''
    }
    
  }

  get id(): number {
    return this._id
  }
}